package top.someapp.applib.cast;


import top.someapp.applib.Base;

/**
 * java 中的基本类型
 *
 * @author zw-zheng
 * Created on 2019-07-19
 */
public enum BasicType implements Base.Describable {
    BYTE("byte/Byte/8bit"),
    SHORT("short/Short"),
    INT("int/Integer/32bit"),
    LONG("long/Long/64bit"),
    FLOAT("float/Float"),
    DOUBLE("double/Double"),
    BOOLEAN("boolean/Boolean"),
    CHAR("char/Character"),
    STRING("String/字符串");

    private static final Class<?>[] boxTypes = {
        Byte.class, Short.class, Integer.class, Long.class,
        Float.class, Double.class,
        Boolean.class,
        Character.class, String.class
    };
    private static final Class<?>[] unBoxTypes = {
        byte.class, short.class, int.class, long.class,
        float.class, double.class,
        boolean.class,
        char.class, String.class
    };
    private final String descn;

    BasicType(String descn) {
        this.descn = descn;
    }

    public static BasicType of(Class clz) {
        if (clz == null) {
            return null;
        }
        for (int i = 0, len = boxTypes.length; i < len; i++) {
            if (boxTypes[i].equals(clz) || unBoxTypes[i].equals(clz)) {
                return BasicType.values()[i];
            }
        }
        return null;
    }

    @Override
    public String describe() {
        return descn;
    }

    /**
     * 对应的包装类型
     *
     * @return 对应的包装类型
     */
    public Class<?> boxType() {
        return boxTypes[ordinal()];
    }

    /**
     * 对应的非包装类型
     *
     * @return 对应的非包装类型
     */
    public Class<?> unBoxType() {
        return unBoxTypes[ordinal()];
    }
}
